//
// Created by kylin on 23-5-10.
//

#include "cJSON.h"
#include <cstring>
#include <ctime>
#include <ostream>
#include "Transcjson.h"

Transcjson::Transcjson(char *token_,int station_) :code(5),token(token_),station(station_){
    time_t t;
    time_=time(&t);
}

std::string Transcjson::deal(char *str,std::string str_cjson) {
    //analyze the data of json from machine
    cJSON *cjson_=NULL;
    cJSON *cjson_code=NULL;
    cJSON *cjson_token=NULL;
    cJSON *cjson_station=NULL;
    cJSON *cjson_time=NULL;
    cJSON *cjson_temperature=NULL;
    cJSON *cjson_humidity=NULL;
    cJSON *cjson_speed=NULL;
    cJSON *cjson_direction=NULL;
    cJSON *cjson_rain;
    cJSON *cjson_sunlight=NULL;
    cJSON *cjson_pm25=NULL;
    cJSON *cjson_pm10=NULL;
    cjson_=cJSON_Parse(str);
    cjson_temperature= cJSON_GetObjectItem(cjson_,"ambientTemperature");
    cjson_humidity=cJSON_GetObjectItem(cjson_,"ambientHumidity");
    cjson_speed= cJSON_GetObjectItem(cjson_,"windSpeed");
    cjson_direction= cJSON_GetObjectItem(cjson_,"windDirection");
    cjson_rain= cJSON_GetObjectItem(cjson_,"rainfall");
    cjson_sunlight= cJSON_GetObjectItem(cjson_,"lightIntensity");
    cjson_pm25= cJSON_GetObjectItem(cjson_,"PM2.5");
    cjson_pm10= cJSON_GetObjectItem(cjson_,"PM10");
    temperature=cjson_temperature->valuedouble;
    humidity=cjson_humidity->valuedouble;
    speed=cjson_speed->valuedouble;
    direction=cjson_direction->valuedouble;
    rain=cjson_rain->valuedouble;
    sunlight=cjson_sunlight->valuedouble;
    pm10=cjson_pm10->valuedouble;
    pm25=cjson_pm25->valuedouble;
    //create a new cjson
    cJSON *cjson_new=NULL;
    cJSON_AddNumberToObject(cjson_new,"code",code);
    cJSON_AddStringToObject(cjson_new,"token",token);
    cJSON_AddNumberToObject(cjson_new,"station",station);
    cJSON_AddNumberToObject(cjson_new,"time",time_);
    cJSON_AddNumberToObject(cjson_new,"temperature",temperature);
    cJSON_AddNumberToObject(cjson_new,"humidity",humidity);
    cJSON_AddNumberToObject(cjson_new,"speed",speed);
    cJSON_AddNumberToObject(cjson_new,"direction",direction);
    cJSON_AddNumberToObject(cjson_new,"rain",rain);
    cJSON_AddNumberToObject(cjson_new,"sunlight",sunlight);
    cJSON_AddNumberToObject(cjson_new,"pm25",pm25);
    cJSON_AddNumberToObject(cjson_new,"pm10",pm10);
    str_cjson= cJSON_Print(cjson_new);
    cJSON_Delete(cjson_);
    cJSON_Delete(cjson_new);
    return str_cjson;
}

//int Transcjson::getCode() const {
//    return code;
//}
//
//int Transcjson::getStation() const {
//    return station;
//}
//
//long Transcjson::getTime() const {
//    return time;
//}
//
//double Transcjson::getTemperature() const {
//    return temperature;
//}
//
//const std::string &Transcjson::getToken() const {
//    return token;
//}
//
//double Transcjson::getHumidity() const {
//    return humidity;
//}
//
//double Transcjson::getSpeed() const {
//    return speed;
//}
//
//double Transcjson::getDirection() const {
//    return direction;
//}
//
//double Transcjson::getRain() const {
//    return rain;
//}
//
//double Transcjson::getSunlight() const {
//    return sunlight;
//}
//
//double Transcjson::getPm25() const {
//    return pm25;
//}
//
//double Transcjson::getPm10() const {
//    return pm10;
//}